﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Mysolo : Form
    {
        private int id;

        public Mysolo()
        {
            InitializeComponent();
        }
        // Name,   Salary    ManagerId
        public Mysolo (int id ,string Name ,int Salary, int ManagerId)
        {
            InitializeComponent();
            this.id= id;
            textBox1.Text = Name;
            textBox1.Text = Salary.ToString();
            textBox1.Text = ManagerId.ToString();

        }

        private void Mysolo_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            /*  SaveFileDialog SaveDate = new SaveFileDialog();//以保存文件的方式打开
              SaveDate.InitialDirectory = @"C:\"; //默认路径 C：\
              SaveDate.Filter = "SQL文件(*.sql)|*.sql";  //保存为sql文件
              string script = "";
              SaveDate.FileName = "repos"; // 默认文件名为repos
              if(SaveDate .ShowDialog() ==DialogResult.OK)
              {
                  //repos赋值为选择保存的路劲// Name,Salary,ManagerId
                  script = SaveDate.FileName;
              }*/
            var SalaryString = textBox2.Text.ToString();
            var ManagerIdString = textBox3.Text.ToString();

            var name = textBox1.Text;
            var age = SalaryString.Count() > 0 ? int.Parse(SalaryString) : 18;
            var Score = ManagerIdString.Count() > 0 ? int.Parse(ManagerIdString) : 0;
            //更新
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(Name))
                {
                    var sql = string.Format("update Student1 set StudentNmae='{0}',Salary={1},ManagerId={2},where id={3}", name, age, Score, this.id);
                    var res = DbHelper.AddOrEditSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("修改成功！", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                } 
            }
            else//增添
            {
                if (!string .IsNullOrEmpty(name))// Name,Salary,ManagerId
                {
                    var sql = string.Format("insert into Student (StudentNmae,Salary ,ManagerId)values('{0}',{1}{2})", name ,age,Score);

                    var res = DbHelper.AddOrEditSave(sql);

                    if (res == 1)
                    {
                        MessageBox.Show("添加成功！", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }/*
                    else if (textBox2.Text == "")
                    {
                        MessageBox.Show("请在年龄框中填入年龄！", "添加成功！");
                        textBox2.Focus();
                    }
                    else if (textBox2.Text == "")
                    {
                        MessageBox.Show("请在成绩框中填入成绩！", "添加成功！");
                        textBox2.Focus();
                    }
                    else
                    {
                        MessageBox.Show("添加失败!", "提示");
                        this.Close();
                    }
                    */
                }
                else
                {
                    MessageBox.Show("请在姓名框中填入姓名！", "提示");
                }
            }
            
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
